#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: 李吉凯
@Email: lijk34925@hundsun.com
@Time: 2023/3/7 10:35
@File: httpRequest.py
"""
from requests import Session
from requests import Response
from BladeFerm.Lib.Common import logger


class HttpRequest(Session):
    """请求类实现"""

    data_type_list = ["DealSql", "data", "json"]

    def __init__(self):
        super().__init__()
        self._last_response = None
        self.headers_cookies = None


    @property
    def response(self) -> Response:
        return self._last_response

    @response.setter
    def response(self, value):
        self._last_response = value

    def send_request(
        self, method, url, params=None, data=None, headers=None, **kwargs
    ):
        req_info = {
            "请求地址": url,
            "请求方法": method,
            "请求头": headers,
            "请求参数": params,
            "请求数据": data
        }
        logger.debug(req_info)
        self.response = self.request(
            method=method, url=url, params=params,  data=data, headers=headers, **kwargs
        )
        rep_info = {
            "响应耗时(ms)": self.response.elapsed.total_seconds() * 1000,
            "状态码": self.response.status_code,
            "响应数据": self.response.json(),
        }
        logger.debug(rep_info)
        if self.response.status_code != 200:
            logger.error("请求发生失败！")

    def doPost(self, url, data=None, headers=None, **kwargs):
        self.send_request(method='post', url=url, params=None, data=data, headers=headers, **kwargs)
        return self.response

    def doGet(self, url, data=None, headers=None):
        self.send_request(method='get', url=url, params=data, data=None, headers=headers)
        return self.response

    def doGetMethod(self, url, params=None, data=None, headers=None):
        self.send_request(method='get', url=url, params=params, data=data, headers=headers)
        return self.response


if __name__ == '__main__':
    url = "http://cloud.blade.hundsun.com/shareInterface/importOfflineCase.json"
    url2 = "http://cloud.blade.hundsun.com/shareInterface/queryCaseStepKey.json"

    params2 = {
        "api_token": "uRlCPWWoqW4j5wshw2xPlq1V-yAegY4vVNTLbgl9NigT2J2M_Uu_p2pbQ42wdseo447mlb_irIWY3H-vgaVKsg",
        "account": "lijk34925",
        "nodePath": "功能案例库/05企业日志查询.可疑交易管理/业务逻辑/可疑交易规则/D012/1020--开户后短期内大量买卖证券，然后迅速销户/基础数据检查（数据库）",
        "data": [
            {
                "caseName": "03.默认启用状态检查-不开启",
                "caseNo": "",
                "caseType": "1",
                "caseState": "0",
                "casePrior": "P2",
                "caseSideType": "0",
                "caseLayer": "0",
                "caseDetailType": "0",
                "caseHeader": "lijk34925",
                "caseDesc": "用例上传验证",
                "step": [
                    {"stepName": "用例上传验证-1",
                        "stepJson": {
                            "stepDes": "fads",
                            "Expect": "aafdasaa",
                            "inputData": "输afdsa入数据",
                            "scriptContent": "a487aab17cca48ccacd8f2235d3b9701"
                        }
                    }
                ]
            }
        ]
    }

    params = {
      "account": "lijk34925",
      "nodePath": "功能案例库/05企业日志查询.可疑交易管理/业务逻辑/可疑交易规则/D012/1020--开户后短期内大量买卖证券，然后迅速销户/基础数据检查（数据库）",
      "data": [
        {
          "caseName": "01.规则基础数据在数据列表中（规则号、版本、行业）",
          "caseNo": "2307106350",
          "caseType": "2",
          "caseState": "1",
          "casePrior": "P1",
          "caseSideType": "0",
          "caseLayer": "1",
          "caseDetailType": "0",
          "caseHeader": "lijk34925",
          "step": [
            {
              "stepName": "01.规则基础数据在数据列表中（规则号、版本、行业）-1",
              "stepJson": {
                "stepDes": "部署hsrcm-amlcom-server服务",
                "Expect": "部署hsrcm-amlcom-server服务",
                "inputData": "部署hsrcm-amlcom-server服务",
                "precisionTest": "2",
                "scriptContent": "a487aab17cca48ccacd8f2235d3b9701"
              }
            },
            {
              "stepName": "01.规则基础数据在数据列表中（规则号、版本、行业）-2",
              "stepJson": {
                "stepDes": "检查可疑规则表amlls_cfg_rule_list、amlls_cfg_rulesql_list、amlls_cfg_ruleparameter_list、amlls_cfg_ruleproposal_list的数据",
                "Expect": "检查可疑规则表amlls_cfg_rule_list、amlls_cfg_rulesql_list、amlls_cfg_ruleparameter_list、amlls_cfg_ruleproposal_list的数据",
                "inputData": "检查可疑规则表amlls_cfg_rule_list、amlls_cfg_rulesql_list、amlls_cfg_ruleparameter_list、amlls_cfg_ruleproposal_list的数据",
                "precisionTest": "2",
                "scriptContent": "c9869783e1704d7caa8648318846bef5",
                "dataContent": {
                  "id": "8964f9b44f104491b93b901719ab4576",
                  "dataChoseRow": "2b478f7430b843ffbeb710f3b4eaa374",
                  "content": "",
                  "dataArrContent": [
                    {
                      "Sheet0": [
                        [
                          "",
                          "序号",
                          "期望",
                          "tables",
                          "rules_no",
                          "rule_version",
                          "finance_industry_class_list"
                        ],
                        [
                          "",
                          "参数说明",
                          "",
                          "",
                          "",
                          "",
                          ""
                        ],
                        [
                          "2b478f7430b843ffbeb710f3b4eaa374",
                          "No.1",
                          "result\u003d1",
                          "amlls_cfg_rule_list,amlls_cfg_rulesql_list,amlls_cfg_ruleparameter_list,amlls_cfg_ruleproposal_list",
                          "D012",
                          "1020",
                          "1"
                        ]
                      ]
                    }
                  ]
                }
              }
            }
          ]
        }
      ],
  "api_token": "uRlCPWWoqW4j5wshw2xPlq1V-yAegY4vVNTLbgl9NigT2J2M_Uu_p2pbQ42wdseo447mlb_irIWY3H-vgaVKsg"
}

    params3 = {
        "api_token": "uRlCPWWoqW4j5wshw2xPlq1V-yAegY4vVNTLbgl9NigT2J2M_Uu_p2pbQ42wdseo447mlb_irIWY3H-vgaVKsg",
        "account": "lijk34925"
    }
    HP = HttpRequest()
    # HP.send_request('GET', url2, data=json.dumps(params3), headers={"Content-Type": "application/json;charset=UTF-8"})
    # HP.doPost(url, data=json.dumps(DealSql), headers={"Content-Type": "application/json;charset=UTF-8"})

    bd = {"title": "test", "content": {"interface": "/api/v1/login", "keys": [{"\\u6d4b\\u8bd5\\u76ee\\u7684": "\\u9a8c\\u8bc1\\u7528\\u6237\\u4f7f\\u7528\\u6b63\\u786e\\u7684\\u7528\\u6237\\u540d\\u548c\\u5bc6\\u7801\\u767b\\u5f55\\u65f6\\u80fd\\u6210\\u529f\\u83b7\\u53d6\\u8ba4\\u8bc1\\u4ee4\\u724c\\u3002", "\\u63a5\\u53e3\\u53c2\\u6570": {"username": "testuser", "password": "securepassword"}}, {"\\u6d4b\\u8bd5\\u76ee\\u7684": "\\u9a8c\\u8bc1\\u8fb9\\u754c\\u503c\\uff0c\\u4f7f\\u7528\\u6700\\u5c0f\\u957f\\u5ea6\\u7684\\u7528\\u6237\\u540d\\u767b\\u5f55\\u3002", "\\u63a5\\u53e3\\u53c2\\u6570": {"username": "us", "password": "securepassword"}}, {"\\u6d4b\\u8bd5\\u76ee\\u7684": "\\u9a8c\\u8bc1\\u8fb9\\u754c\\u503c\\uff0c\\u4f7f\\u7528\\u6700\\u5927\\u957f\\u5ea6\\u7684\\u7528\\u6237\\u540d\\u767b\\u5f55\\u3002", "\\u63a5\\u53e3\\u53c2\\u6570": {"username": "u", "password": "securepassword"}}, {"\\u6d4b\\u8bd5\\u76ee\\u7684": "\\u9a8c\\u8bc1\\u8fb9\\u754c\\u503c\\uff0c\\u4f7f\\u7528\\u6700\\u5c0f\\u957f\\u5ea6\\u7684\\u5bc6\\u7801\\u767b\\u5f55\\u3002", "\\u63a5\\u53e3\\u53c2\\u6570": {"username": "testuser", "password": "se"}}, {"\\u6d4b\\u8bd5\\u76ee\\u7684": "\\u9a8c\\u8bc1\\u8fb9\\u754c\\u503c\\uff0c\\u4f7f\\u7528\\u6700\\u5927\\u957f\\u5ea6\\u7684\\u5bc6\\u7801\\u767b\\u5f55\\u3002", "\\u63a5\\u53e3\\u53c2\\u6570": {"username": "testuser", "password": "p"}}]}}
    HP.send_request("post", "http://10.188.119.162:8080/aigenerate", data=bd)